package bai2.com.client;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import bai2.com.server.MyRemoteServer;
import bai2.com.server.ServerCommandInterface;

public class ClientGUI extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6488606919681822313L;
	private JPanel contentPane;
	private JTextField txtPath;
	private JFileChooser fileChooser;
	private File fileToUpload;
	private JButton btnUpload;
	private JButton btnBrowse;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ClientGUI frame = new ClientGUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 118);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtPath = new JTextField();
		txtPath.setBounds(10, 12, 301, 20);
		contentPane.add(txtPath);
		txtPath.setColumns(10);

		btnBrowse = new JButton("Browse");
		btnBrowse.addActionListener(onClickBrowse);
		btnBrowse.setBounds(335, 11, 89, 23);
		contentPane.add(btnBrowse);

		btnUpload = new JButton("Upload");
		btnUpload.addActionListener(onClickUpload);
		btnUpload.setBounds(168, 43, 89, 23);
		contentPane.add(btnUpload);
	}

	private ActionListener onClickUpload = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				ServerCommandInterface commandInterface = (ServerCommandInterface) Naming
						.lookup(MyRemoteServer.REMOTE_SERVER);
				commandInterface.upload(fileToUpload);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NotBoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

	private ActionListener onClickBrowse = new ActionListener() {
		public void actionPerformed(ActionEvent arg0) {
			fileChooser = new JFileChooser();
			int returnVal = fileChooser.showOpenDialog(ClientGUI.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				fileToUpload = fileChooser.getSelectedFile();
				txtPath.setText(fileToUpload.getAbsolutePath());
			}
		}
	};
}
